package org.atlantis.ymer;

public class DisconnectReason {

	public static final DisconnectReason NORMAL_DISCONNECT = new DisconnectReason(DisconnectType.NORMAL, null, true);
	public static final DisconnectReason CLOSE_DISCONNECT = new DisconnectReason(DisconnectType.CHANNEL_CLOSE, null, false);
	public static final DisconnectReason TIMEOUT_DISCONNECT = new DisconnectReason(DisconnectType.TIMEOUT, null, true);

	public static enum DisconnectType {
		/** 正常断开，服务端或客户端发出断开请求 */
		NORMAL,
		/** 服务端发生异常断开 */
		EXCEPTION,
		/** 网络断开（如网页刷新，浏览器关闭等） */
		CHANNEL_CLOSE,
		/** 心跳超时断开 */
		TIMEOUT;
	}

	private DisconnectType type;
	private Object reason;
	private boolean noticeClient;

	private DisconnectReason(DisconnectType type, Object reason, boolean noticeClient) {
		this.type = type;
		this.reason = reason;
		this.noticeClient = noticeClient;
	}

	public static DisconnectReason createException(Throwable cause) {
		return new DisconnectReason(DisconnectType.EXCEPTION, cause, true);
	}

	public static DisconnectReason createException() {
		return createException(null);
	}

	public DisconnectType getType() {
		return type;
	}

	public Object getReason() {
		return reason;
	}

	public boolean isNoticeClient() {
		return noticeClient;
	}
}
